<?php

namespace Hyperfx\Utils;

use Hyperf\Database\Model;

class ProtobufUtil
{

    public  static function repeatedForeach(\Google\Protobuf\Internal\RepeatedField $data, callable $handle): array {
        $result = [];
        if ($data->count() == 0) {
            return $result;
        }
        foreach ($data as $item) {
            $result[] = $handle($item);
        }
        return $result;
    }

    public static function repeatedForeachAutoGenArray(\Google\Protobuf\Internal\RepeatedField $data): array {
        $inited = false;//是否初始化
        $properties = [];//类成员
        $propMethodMap = [];//成员方法映射
        // 初始化func
        $initFunc = function($repeatedObj) use(&$inited, &$properties, &$propMethodMap): void {
            $ref = new \ReflectionClass($repeatedObj);
            foreach ($ref->getProperties() as $property) {
                $properties[] = $property->getName();
            }
            foreach ($ref->getMethods() as $method) {
                foreach ($properties as $property) {
                    // 获取映射关系
                    if (0 == strcmp(strtolower($method->getName()), 'get'. strtolower($property))) {
                        $propMethodMap[] = [$property, $method->getName()];
                    }
                }
            }
            $inited = true;
        };
        // 处理对象func
        $handleObjFunc = function($repeatedObj) use($initFunc, &$inited, &$propMethodMap) : array {
            if (! $inited) {
                $initFunc($repeatedObj);
            }
            $result = [];
            foreach ($propMethodMap as [$property, $method])
            {
                // 根据映射关系调用方法赋值给成员
                $result[$property] = $repeatedObj->{$method}();
            }
            return $result;
        };
        $result = [];
        foreach ($data as $item) {
            $row = $handleObjFunc($item);
            $result[] = $row;
        }
        return $result;
    }

    public static function repeatedToStringArray(\Google\Protobuf\Internal\RepeatedField $data): array {
        $result = [];
        if ($data->count() == 0) {
            return $result;
        }
        foreach ($data as $item) {
            $result[] = (string) $item;
        }
        return $result;
    }

    public static function repeatedToIntArray(\Google\Protobuf\Internal\RepeatedField $data): array {
        $result = [];
        if ($data->count() == 0) {
            return $result;
        }
        foreach ($data as $item) {
            $result[] = (int) $item;
        }
        return $result;
    }

    public static function foreachX($models, callable $callback) :array  {
        $list = [];
        if (empty($models)) {
            return $list;
        }
        if ($models instanceof Model\Collection && $models->count() == 0) {
            return $list;
        }
        foreach ($models as $model) {
            $list[] = $callback($model);
        }
        return $list;
    }

    public static function boolToStringStatus(bool $val): string
    {
        if ($val) {
            return 'Y';
        }
        return 'N';
    }

    public static function stringStatusToBool(string $val): bool
    {
        if (0 === strcmp($val, 'Y')) {
            return true;
        }
        return false;
    }
}